/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.web;

import com.vce.User;
import com.vce.election.*;

/**
 * @author Ggutierrez
 */
public class LoginBean extends ManagedBean{

    private String username;
    private String password;
    private boolean validPassword;

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    public String login() {
        Election election = getElection();
        User user = election.authenticate(username, password);
        if(user == null){
            addErrorMessage("Usuario o password invalidos");
            getSecurityManager().setCurrentUser(null);
            return "fail";
        }
        getSecurityManager().setCurrentUser(user);
        validPassword = true;
        release();
        
        return "success";
    }
    
    public String logout(){
        getSecurityManager().clearContext();
        return "success";
    }

    public void release() {
        username = null;
        password = null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isValidPassword() {
        return validPassword;
    }

    public void setValidPassword(boolean validPassword) {
        this.validPassword = validPassword;
    }
}
